package mongopkg

import (
	"context"
	"fmt"
	"mygame/config"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

var (
	Client *mongo.Client
	DB     *mongo.Database
	Ctx    context.Context
	Err    error
)

func init() {
	uri := fmt.Sprintf("mongodb://%s:%s@%s:%d",
		// "root",
		// "rTFfgSHaTCT6",
		// "localhost",
		// 13303,
		config.App.Mongo.Username,
		config.App.Mongo.Password,
		config.App.Mongo.Host,
		config.App.Mongo.Port,
	)
	clientOptions := options.Client().ApplyURI(uri)
	Client, Err = mongo.Connect(context.TODO(), clientOptions)
	if Err != nil {
		fmt.Println("mongodb init err")
		panic(Err)
	}
	// 检查连接
	Err = Client.Ping(context.TODO(), nil)
	if Err != nil {
		fmt.Println("mongodb Ping err")
		panic(Err)
	}
	DB = Client.Database(config.App.Mongo.Database)
	fmt.Println("Connected to MongoDB")
}
